//-----------------------------------------------------------------------------
// Bend2D - Physics Engine
// Roberto Bender <beto.bender@gmail.com>
// Description: 
// Date: 20/03/2010
//-----------------------------------------------------------------------------
#include "collideregions.h"
#include "world.h"
#include "bmath.h"
#include "moveable.h"

using namespace bend2d;

void CollideRegion::AddColliableObject(Collidable* object)
{
    if(m_AddedObjects.find(object) == m_AddedObjects.end())
        m_CollidableObjects.push_back(object);
    m_AddedObjects[object] = true;
}

list<Collidable*>& CollideRegion::GetObjects()
{
    return m_CollidableObjects;
}

const list<Collidable*>& CollideRegion::GetObjects() const
{
    return m_CollidableObjects;
}

void CollideRegions::Reset()
{
    //m_WillCollide.clear();
}

void CollideRegions::SetWorld(World* world) 
{
    m_World = world;
    Reset();
}

void CollideRegions::RecreateRegions(list<Collidable*>& regions)
{
    Reset();
    Collidable::CollidableList& collidablelist = m_World->GetCollidableList();
    for(Collidable::CollidableList::iterator it = collidablelist.begin(); it != collidablelist.end(); ++it)
    {
        Collidable::CollidableList::iterator againstIt = it;

        for(++againstIt; againstIt != collidablelist.end(); ++againstIt)
        {
            Collidable* objectA = *it;
            Collidable* objectB = *againstIt;

            if(objectA->CanCollide(objectB))
                objectA->Collide(objectB);
        }
    }
}

void CollideRegion::SetRegionRect(const bend2d::Rectangle& rect)
{
    m_RegionRect = rect;
}

const bend2d::Rectangle& CollideRegion::GetRegionRect() const
{
    return m_RegionRect;
}
